package vcp.client.GUI.mvc.controller;

import javax.swing.SwingUtilities;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.model.UIAbstractModel;
import vcp.client.GUI.mvc.model.UIInfoRetrieveModel;
import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.GUI.mvc.view.UIInfoRetrieveView;
import vcp.client.forms.ReportSummaryForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.entity.Worker;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         responsible for retrieving readonly information for CEO
 *         </p>
 * 
 * @see UIInfoRetrieveView
 * @see UIInfoRetrieveModel
 * @see IFormSubmitionListener
 */
public class UIInfoRetrieveController extends UIAbstractController implements IFormSubmitionListener <ReportSummaryForm>
{
	private final UIInfoRetrieveView view;
	private final UIInfoRetrieveModel model;

	public UIInfoRetrieveController ( UIInfoRetrieveView view, UIInfoRetrieveModel model )
	{
		super(Screen.INFORMATION);
		this.view = view;
		this.model = model;
		this.setupListeners();
	}

	// ****************************************************************************************************************************
	// UIAbstractController methods
	// ****************************************************************************************************************************

	@Override
	public UIAbstractView getView ()
	{
		return this.view;
	}

	@Override
	public UIAbstractModel getModel ()
	{
		return this.model;
	}

	@Override
	public void clearMVC ()
	{
		this.view.setOnScreen(false);
	}

	@Override
	public void flashData ()
	{
		this.model.clearModel();
		this.view.clearView();
	}

	@Override
	public void uploadDataContext ( Object data )
	{
		if ( data instanceof Worker )
		{
			Worker CEO = (Worker) data;

			this.model.setCEO(CEO);
			this.view.setOnScreen(true);
		}
	}

	@Override
	protected synchronized void reloadGUI ()
	{
		this.view.loadReadOnlyReport(this.model.getReport());
	}

	@Override
	protected void setupListeners ()
	{
		super.setupListeners();
		this.view.setFormSubmitionListener(this);
	}

	// ****************************************************************************************************************************
	// IControllerModelDelegate methods ***
	// ****************************************************************************************************************************
	@Override
	public void updateControllerDelegateWithData ( Object newData )
	{
		if ( this.view.isOnScreen() )
		{
			SwingUtilities.invokeLater(new Runnable()
				{
					@Override
					public void run ()
					{
						UIInfoRetrieveController.this.reloadGUI();
					}
				});
		}
	}

	// ****************************************************************************************************************************
	// IFormSubmitionListener methods
	// ****************************************************************************************************************************
	@Override
	public void onFormSubmitted ( FormSubmitionEvent <ReportSummaryForm> event )
	{
		this.model.retrieveInforamtion(event.getForm());
	}
}
